<template>
  <Row type="flex" justify="space-around" class="code-row-bg">
    <Form ref="selfCheckForm" :model="dataClone" :rules="ruleCustom" class="self-check-form w" label-position='right' :show-message="false">
      <Row type="flex" justify="space-around" class="code-row-bg">
        <Col span="23">
          <FormItem label="企业名称：" prop="" :label-width="125">
              <div class='w'>{{dataClone.belongOrgName}}</div>
          </FormItem>
        </Col>
      </Row>
      <Row type="flex" justify="space-around" class="code-row-bg">
        <Col span="7">
          <FormItem label="现场审核时间：" prop="" :label-width="125">
              <div class='w'>{{dataClone.auditTime.substr(0,10)}}</div>
          </FormItem>
        </Col>
        <Col span="7">
          <FormItem label="审核人：" prop="" :label-width="125">
              <div class='w'>{{dataClone.auditorName}}</div>
          </FormItem>
        </Col>
        <Col span="7">
          <FormItem label="企业主管：" prop="" :label-width="125">
              <div class='w'>{{dataClone.managerName}}</div>
          </FormItem>
        </Col>
      </Row>
     <!-- <Row type="flex" justify="space-around" class="code-row-bg">
        <Col span="23">
          <FormItem label="自查存在的问题：" prop="" :label-width="125">
              <Table :columns="problemColumns" :data="dataClone.selfCheckItems"  :content="self">
              </Table>
          </FormItem>
        </Col>
      </Row>-->
      <Row type="flex" justify="space-around" class="code-row-bg">
        <Col span="23">

          <FormItem label="现场审核中发现的其他不符合项：" prop="" :label-width="125">
              <Table :columns="otherProblemColumns" :data="dataClone.sceneCheckItems":content="self">
              </Table>
          </FormItem>
        </Col>
      </Row>
      <Row type="flex" justify="space-around" class="code-row-bg">
        <Col span="23">

          <FormItem label="是否存在否决项：" prop="" :label-width="125">
              <div class='w'>{{dataClone.hasVeto==1?'是':'否'}}</div>
          </FormItem>
        </Col>
      </Row>
      <Row type="flex" justify="space-around" class="code-row-bg">
        <Col span="23">
          <FormItem label="否决项内容：" prop="" v-if="dataClone.hasVeto==1" :label-width="125">
              <div class='w'>{{dataClone.vetoContent}}</div>
          </FormItem>
          <FormItem label="综合评定意见：" prop=""></FormItem>
        </Col>
      </Row>

      <Row type="flex" justify="space-around" class="code-row-bg">
        <Col span="11">
          <FormItem label="评定得分：" prop="evaluationScore">
            <Input type="text" v-model="dataClone.evaluationScore"></Input>
          </FormItem>
        </Col>
        <Col span="11">
          <FormItem label="涉及得分：" prop="involvedScore">
            <Input type="text" v-model="dataClone.involvedScore"></Input>
          </FormItem>
        </Col>
      </Row>
      <Row type="flex" justify="space-around" class="code-row-bg">
        <Col span="11">
          <FormItem label="综合评定得分：" prop="integretedScore">
            <Input type="text" v-model="dataClone.integretedScore"></Input>
          </FormItem>
        </Col>
        <Col span="11">
        <FormItem label="评定等级：" prop="level">
          <Select class='w' v-model="dataClone.level" :disabled="true">
            <Option v-for="item in enStdMap" :value="item.codingCodeId" :key="item.codingCodeId">{{ item.codingName }}
            </Option>
          </Select>
        </FormItem>
        </Col>
      </Row>
    </Form>
  </Row>

</template>

<script>
import {Api} from "@/api";
var cascaderDom = () => import('@/components/cascader-dom.vue');
import _deepclone from 'lodash/cloneDeep'
function countLevel(newVal) {
    var map = this.enStdMap
    var level = 1
    var score = this.dataClone.integretedScore
    if (370 > score /*|| this.dataClone.hasVeto == 1 */) {
        level = 4
    } else if (score > 470) {
        level = 1
    } else if (470 >= score && score >= 420) {
        level = 2
    } else if (419 >= score && score >= 370) {
        level = 3
    }
    this.$set(this.dataClone, 'level', map[level].codingCodeId)
}
export default {
  name: "Result",
  props:['data','enStdMap','mode'],
  data(){
    return {
      dataClone:{},
      self: this,
      problemColumns:[
        { title: '序号', key: 'seqNo', align: 'center', width: 60 },
        { title: '评价内容',  key: 'content', align: 'center', width: 140 },
          {title: '评分标准', key: 'standard', align: 'center', width: 140, tooltip:true},
        { title: '分值', key: 'score', width: 65, align: 'center', },
          {title: '涉及得分', key: 'ifInvolved', width: 65, align: 'center',
              render:(h, params)=>{ return h('span', [ (!params.row.score || !params.row.ifInvolved) ? '' : params.row.score ]) }},
          { title: '存在的问题', key: 'problem', align: 'center' },
        { title: '自评得分', key: 'ratingScore', width:65, align: 'center',
            render:(h, params)=>{ return h('span', [ (!params.row.score || !params.row.ifInvolved) ? '' : params.row.ratingScore ]) }},
          { title: '整改情况', key: 'rectificationDetail',minWidth: 145,  align: 'center' },
        { title: '评定扣分', key: 'deductScore', width:65, align: 'center',
            render:(h, params)=>{
                return h('span', [
                    ((!params.row.score || !params.row.ifInvolved) ? '' : (params.row.ratingScore === 0 ? params.row.score : (params.row.score===params.row.ratingScore?0:params.row.score-params.row.ratingScore)))
                ]);
            }},
      ],
      otherProblemColumns:[
        { title: '序号', key: 'seqNo', align: 'center', width: 100  },
        { title: '评价内容',  key: 'content', align: 'center', width: 140},
          {title: '评分标准', key: 'standard', align: 'center', width: 160, tooltip:true},
        { title: '分值', key: 'score', width:80, align: 'center' },
          {title: '涉及得分', key: 'ifInvolved', width: 65, align: 'center',
              render:(h, params)=>{ return h('span', [ (!params.row.score || !params.row.ifInvolved) ? '' : params.row.score ]) }},
        { title: '存在的问题', key: 'problem', align: 'center' },
        { title: '评定扣分', key: 'deductScore', width:90, align: 'center' ,
            render:(h, params)=>{ return h('span', [ (!params.row.score || !params.row.ifInvolved) ? '' : params.row.deductScore ]) }},
      ],
      ruleCustom : {
        belongOrgId: [ { required: true, message: '请选择企业名称', trigger: 'blur' } ],
        auditTime: [ { required: true, message: '请选择现场审核时间', trigger: 'blur' } ],
        auditor: [ { required: true, message: '请选择审核人', trigger: 'blur' } ],
        manager: [ { required: true, message: '请选择企业主管', trigger: 'blur' } ],
        selfCheckItems: [{
          validator: (rule, value, callback) => {
            for (let i=0,il=dataClone.selfCheckItems.length;i<il;i++) {
              let obj = dataClone.selfCheckItems[i]
              if (obj.problem==='' || obj.deductScore==='' || obj.itemId==='' || obj.ratingScore==='' || obj.rectificationDetail==='') {
                callback(new Error('请填写自查问题表单中的每一项'));
                return
              }
            }
            callback();
          }, trigger: 'change'
        }],
        sceneCheckItems: [{
          validator: (rule, value, callback) => {
            for (let i=0,il=dataClone.sceneCheckItems.length;i<il;i++) {
              let obj = dataClone.sceneCheckItems[i]
              if (obj.problem==='' || obj.deductScore==='' || obj.itemId==='' ) {
                callback(new Error('请填写其他问题表单中的每一项'));
                return
              }
            }
            callback();
          }, trigger: 'change'
        }],
        hasVeto: [ { required: true, message: '请选择是否存在否决项', trigger: 'change' } ],
        vetoContent: [ { required: true, message: '请输入否决项', trigger: 'change' } ],
        evaluationScore: [
          {required: true, message: '请输入评定得分', trigger: 'change'},
          {pattern: /^\d+$/,message: '评定得分必须为数字', trigger: 'change'}
        ],
        involvedScore: [ { required: true, message: '请输入涉及得分', trigger: 'change' },
          {pattern: /^\d+$/,message: '涉及得分必须为数字', trigger: 'change'} ],
        integretedScore: [ { required: true, message: '请输入综合评定得分', trigger: 'change' },
          {pattern: /^\d+$/,message: '综合评定得分必须为数字', trigger: 'change'} ],
        level: [ { required: true, message: '请输入评定等级', trigger: 'blur' } ]
      },
      sending:false
    }
  },
  watch: {
      'dataClone.hasVeto': countLevel,
      'dataClone.integretedScore': countLevel,
  },
  created() {
    if(this.mode === 'add'){
      this.dataClone = {
        auditTime:'',
        hasVeto:'',
        selfCheckItems: [],
        sceneCheckItems: [],
      }
    }else{
      let dataClone = _deepclone(this.data)
      if(!(dataClone.selfCheckItems instanceof Array)){
        dataClone.selfCheckItems = []
      }
      if(!(dataClone.sceneCheckItems instanceof Array)){
        dataClone.sceneCheckItems = []
      }
      dataClone.evaluationScore = (dataClone.evaluationScore!==undefined)?String(dataClone.evaluationScore):''
      dataClone.involvedScore = (dataClone.involvedScore!==undefined)?String(dataClone.involvedScore):''
      dataClone.integretedScore = (dataClone.integretedScore!==undefined)?String(dataClone.integretedScore):''
      this.dataClone = dataClone
    }
    window.dataClone = this.dataClone
  },
  mounted() {
    if(this.mode === 'edit'){
      this.$Spin.show()
      this.$http.get(Api.getEnvSelfCheck+this.data.id, {params: {}}).then((res) => {
        if (res.data.status === 201) {
          this.$Message.error(res.data.msg);
        }else{
          this.dataClone.selfCheckItems = res.data.data.selfCheckItems
          this.dataClone.sceneCheckItems = res.data.data.sceneCheckItems
        }
        this.$Spin.hide()
      }).catch(e => {
        this.$Message.error("自评台账详情数据获取失败")
        this.$Spin.hide()
      })
    }
  },
  computed:{
  },
  methods:{
    // 点击取消。发出关闭弹出框事件
    handleCancel(){
      this.$emit('close-dialog')
    },
    // 发出保存成功事件
    handleSaveSuccess(){
      this.$emit('save-success')
    },
    setOrgId(data){
      this.dataClone.belongOrgId = data[data.length - 1];
    },
    setAuditTime(date){
      this.dataClone.auditTime = date
    },
    clearAuditTime(){
      this.dataClone.auditTime = ''
    },
    setAuditor(data){
      this.dataClone.auditor = data[data.length - 1];
    },
    setManager(data){
      this.dataClone.manager = data[data.length - 1];
    },
    // 清空否决项
    cleanVetoContent(value){
      if(value == 0){
        this.dataClone.vetoContent = '';
      }
    },
    // 一行自评相关操作
    addRowStandard(type){
      this.dataClone[type].push({
        itemId:'',
        standard: '',
        num:'',
        score:'',
        problem:'',
        ratingScore:undefined,
        rectificationDetail:undefined,
        deductScore:'',
      })
    },
    removeRowStandard(type,index){
      this.dataClone[type].splice(index,1)
      this.$refs['selfCheckForm'].validateField(type)
    },
    // 提交
    submit(name) {
      if (this.sending) return
      this.$refs[name].validate((valid) => {
        if (valid) {
          let url = Api.updateEnvSelfCheck
          if(this.mode === 'add'){
            url = Api.saveEnvSelfCheck
          }
          this.sending = true
          this.$http.post(url, this.dataClone).then((res) => {
            if (res.data.status === 200) {
              this.$Message.success(this.mode==='add'?'添加成功':'编辑成功');
              this.handleSaveSuccess()
            }else if (res.data.status === 201) {
              this.$Message.error(res.data.msg);
            }
            this.sending = false
          }).catch(e => {
            this.$Message.error("提交失败")
            this.sending = false
          })
        } else {
          this.$Message.error('请填报完整!');
        }
      })
    }
  },
  components:{
    cascaderDom
  },
}
</script>

<style scoped>
    .mb10{margin-bottom: 10px;}
    .aR{text-align: right;}
    .fixed:after{display: block;content: '';clear: both;width: 100%;height: 0;}
</style>
